package base.tag;

import java.util.ArrayList;
import java.util.LinkedHashMap;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.Tag;

public class BsCarouselTag extends BsContainerTag<LinkedHashMap<String, String>> {

	private static final long serialVersionUID = 1L;
	
	public BsCarouselTag() {
		items = new ArrayList<LinkedHashMap<String, String>>();
	}

	@Override
	public int doStartTag() throws JspException {
		items.clear();
		StringBuffer tag = new StringBuffer();
		tag.append("<div class='col-md-" + colWidth + "'>\n");
		tag.append("<div id='" + name + "' class='carousel slide' data-ride='carousel' style='padding: 10px;margin-bottom:10px;'>\n");
		output(tag);
		return Tag.EVAL_PAGE;
	}
	
	@Override
	public int doEndTag() throws JspException {
		
		StringBuffer tag = new StringBuffer();
		tag.append("<ol class='carousel-indicators'>\n");
		tag.append("<li data-target='#" + name + "' data-slide-to='0' class='active'></li>\n");
		for(int i = 1; i < items.size(); i++)
		{
			tag.append("<li data-target='#" + name + "' data-slide-to='" + i + "'></li>\n");
		}
		tag.append("</ol>");
		
		tag.append("<div class='carousel-inner' role='listbox'>\n");
		tag.append("<div class='item active'>\n"
				 + "<img src='" + items.get(0).get("img") + "' alt='...'>\n"
				 + "<div class='carousel-caption'>" + items.get(0).get("caption") + "</div>\n"
				 + "</div>\n");
		for(int i = 1; i < items.size(); i++)
		{
			tag.append("<div class='item'>\n"
					 + "<img src='" + items.get(i).get("img") + "' alt='...'>\n"
					 + "<div class='carousel-caption'>" + items.get(i).get("caption") + "</div>\n"
					 + "</div>\n");
		}
		tag.append("</div>\n");
		
		tag.append("<a class='left carousel-control' href='#" + name + "' role='button' data-slide='prev'>\n");
		tag.append("<span class='glyphicon glyphicon-chevron-left' aria-hidden='true'></span>\n");
		tag.append("<span class='sr-only'>上一</span>\n");
		tag.append("</a>\n");
		tag.append("<a class='right carousel-control' href='#" + name + "' role='button' data-slide='next'>\n");
		tag.append("<span class='glyphicon glyphicon-chevron-right' aria-hidden='true'></span>\n");
		tag.append("<span class='sr-only'>下一</span>\n");
		tag.append("</a>\n");
		tag.append("</div>");
		tag.append("</div>");
		output(tag);
		return Tag.EVAL_PAGE;
	}
	
}
